package com.takeoutsystem.service.impl;

import com.takeoutsystem.bean.StorageRecord;
import com.takeoutsystem.bean.StorageRemain;
import com.takeoutsystem.dao.TakeoutHistoryMapper;
import com.takeoutsystem.service.TakeoutHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class TakeoutHistoryServiceImpl implements TakeoutHistoryService {

    @Autowired
    TakeoutHistoryMapper mapper;
    @Override
    public boolean addNewHistory(StorageRemain remain,String userId) {
        StorageRecord record=new StorageRecord();
        record.setCabinetId(remain.getCabId());
        record.setInTime(remain.getInTime());
        record.setUserTel(remain.getUserTel());
        record.setTakeUserId(userId);
        int result=mapper.addRecord(record);
        return result>0;
    }

    @Override
    public int countAllRecords() {
        return mapper.selectAll().size();
    }
    @Override
    public int[] countByWeek() {
        int[] weekCountList=new int[7];
        List<StorageRecord> history=mapper.selectAll();
        history.forEach(
                (item)->{
                    Date time=item.getOutTime();
                    Calendar cal=Calendar.getInstance();
                    cal.setTime(time);
                    int index= cal.get(Calendar.DAY_OF_WEEK)-1;
                    index=(index+6)%7;
                    weekCountList[index]++;
                }
        );
        return weekCountList;
    }
}
